﻿package ;

import flash.Boot;
import flash.display.Loader;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.Lib;
import flash.net.NetConnection;
import flash.net.Responder;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import haxe.io.Bytes;
import haxe.Serializer;
import haxe.Unserializer;

/**
 * ...
 * @author waneck
 */

class Main 
{
	static private var nc:NetConnection;
	static private var loader:Loader;
	
	static function main() 
	{
		Boot.getTrace().selectable = true;
		Boot.getTrace().mouseEnabled = true;
		nc = new NetConnection();
		nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
		nc.connect("rtmp:/serializer");
		loader = new Loader();
	}
	
	static private function onNetStatus(e:NetStatusEvent):Void 
	{
		nc.call("getImage", new Responder(getImage), "imagem.jpg");
	}
	
	static private function getImage(img:String):Void
	{
		trace("LOADED");
		var a:Bytes = Unserializer.run(img);
		trace(a.length);
		
		nc.call("setImage", null, Serializer.run(a));
		
		loader.loadBytes(a.getData());
		Lib.current.addChild(loader);
	}
	
}